<?php
	
	require 'connection.php';
	
	if(isset($_POST['user']) && isset($_POST['pass']))
	{
		if(createAcc($_POST['user'], $_POST['pass'], $_POST['money'], $db_connect))
		{
			
			echo "<div id='main'>";
			echo "<h1>Account created!</h1>";
			echo "<a href='login.php'><input type='button' value='Login Here'></a>";
			echo "</div>";
			$_GET['action'] = 'registered';
			
		} else 
		{
			unset($_GET['action']);
		}
	} else 
	{
		$_SESSION['error'] = 'You need to enter username and password!';
		unset($_GET['action']);
	}
?>
<html>
<head>
	<title>Register</title>
	<link type="text/css" href="style/form_css.css" rel="stylesheet"/>
</head>
<body>
<?php 	
	if(isLogged())
	{
		$_SESSION['error'] = ' ';
		echo "<div id='main'>";
		echo "<h2>Already registered</h2>";
		echo "<h4>You are logged in as: ". $_SESSION['username'] . "</h4>"; 
		echo "<a href='index.php'><input type='button' value='Index'></a>";
		echo "<a href='login.php?action=logout'><input type='button' value='Logout'></a>";
		echo "</div>";
		
	}elseif(!isset($_GET['action']))
	{
		echo "<div id='main'>";
		echo "<h2>Register to the system</h2>";
		echo "<form name='register' method='post'>";
		echo "<p>Username: <input name='user'></p>";
		echo "<p>Password: <input type='password' name='pass'></p>";
		echo "<p>Money: <input name='money'></p>";
		echo "<p><input type='submit' name='submit' value='Register!'></p>";
		echo "<a href='login.php'><input type='button' value='Login Here'></a>";
		echo "</div>";
	}

	echo "<div id='error'>";
	echo $_SESSION['error'];
	echo "</div>";
	
?>

</body>
</html>

<?php 
	mysqli_close($db_connect);
?>